package com.zito.health.controller;

/**
 * Created by Administrator on 2019/11/17.
 */

import com.alibaba.fastjson.JSONArray;
import com.zito.health.common.enums.ResultCode;
import com.zito.health.common.result.PlatformResult;
import com.zito.health.entity.Item;
import com.zito.health.entity.ItemExpertConclusion;
import com.zito.health.entity.ItemExpertDeclare;
import com.zito.health.service.ItemExpertConclusionService;
import com.zito.health.service.ItemExpertDeclareService;
import com.zito.health.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目专家评审
 */
@Slf4j
@RestController
@RequestMapping("/item/expertDeclare")
public class ItemExpertDeclareController {

    @Autowired
    private ItemExpertDeclareService itemExpertDeclareService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemExpertConclusionService itemExpertConclusionService;

    /**
     * 分配专家
     * sun updateTime 2020年6月20日17:30:05
     * @param itemIds
     * @param ids
     * @return
     */
    @PostMapping("/save")
    public PlatformResult save(@RequestParam(value = "itemIds") String itemIds, @RequestParam(value = "ids") String ids) {
        String[] idsStr = ids.split(",");
        if(itemIds !=null &&!"".equals(itemIds)){
            String[] itemIdsStr = itemIds.split(",");
            // 查询结题还是立项
            Integer types = itemService.queryById(Integer.valueOf(itemIdsStr[0])).getTypes();
            for (String itemId : itemIdsStr) {
                Item item = new Item();
                item.setId(Integer.parseInt(itemId));
                item.setStates(2);
                itemService.update(item);
                // 立项才可以生成立项专家评审
                if(types == 1) {
                    itemExpertDeclareService.save(Integer.parseInt(itemId), ids);
                }else{
                    itemExpertConclusionService.save(Integer.parseInt(itemId), idsStr);
                }
            }
            return PlatformResult.success();
        }else {
            return PlatformResult.failure(ResultCode.DATA_IS_WRONG);
        }

    }

    /**
     * 修改专家评审意见
     *
     * @param jsonParam
     * @return
     */
    @PostMapping("/update")
    public PlatformResult save(String jsonParam) {
        List<ItemExpertDeclare> items = JSONArray.parseArray(jsonParam, ItemExpertDeclare.class);
        for (ItemExpertDeclare item : items) {
            if (item.getId() != null) {
                itemExpertDeclareService.update(item);
            }
//            else{
//                itemExpertDeclareService.save(item);
//            }
        }
        return PlatformResult.success();
    }

    /**
     * 专家评审意见结果列表（不知道哪里用的，先留着）
     *
     * @param itemExpertDeclare
     * @return
     */
    @PostMapping("/list")
    public PlatformResult list(ItemExpertDeclare itemExpertDeclare) {
        return PlatformResult.success(itemExpertDeclareService.queryList(itemExpertDeclare));
    }

    /**
     * 专家评审意见结果列表（反显）/ sun uodateTime 2020年6月20日10:37:36
     *
     * @param itemId
     * @return
     */
    @PostMapping("/listDetail")
    public PlatformResult listDetail(Integer itemId,Integer expertId) {
        ItemExpertDeclare itemExpertDeclare=new ItemExpertDeclare();
        itemExpertDeclare.setItemId(itemId);
        itemExpertDeclare.setExpertId(expertId);
        // 查询当前专家自己的评审
        return PlatformResult.success(itemExpertDeclareService.listDetail(itemExpertDeclare));
    }

}
